import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/team-management',
    component: Layout,
    name: 'TeamManagement',
    meta: { title: 'TeamManagement', icon: 'dashboard' },
    children: [
      {
        path: 'team',
        component: () => import('@/views/team-management/team-register/index'),
        name: '注册申请',
        meta: { title: '注册申请', icon: 'dashboard' }
      },
      // {
      //   path: 'player',
      //   component: () => import('@/views/team-management/team-register/index'),
      //   name: 'Player',
      //   meta: { title: 'Player', icon: 'dashboard' }
      // },
      {
        path: 'register-check',
        component: () => import('@/views/team-management/register-check/index'),
        name: 'RegisterCheck',
        meta: { title: 'RegisterCheck', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/review-set',
    component: Layout,
    children: [
      {
        path: 'set',
        component: () => import('@/views/review-set/index'),
        name: 'ReviewSet',
        meta: { title: 'ReviewSet', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/review-quiz',
    component: Layout,
    name: 'ReviewQuiz',
    meta: { title: 'ReviewQuiz', icon: 'dashboard' },
    children: [
      {
        path: 'quiz',
        component: () => import('@/views/review-quiz/quiz/index'),
        name: 'Quiz',
        meta: { title: 'Quiz', icon: 'dashboard' }
      },
      {
        path: 'analyze',
        hidden: true,
        component: () => import('@/views/review-quiz/analyze/index'),
        name: 'Analyze',
        meta: { title: 'Analyze', icon: 'dashboard' }
      },
      {
        path: 'wrong',
        component: () => import('@/views/review-quiz/wrong/index'),
        name: 'Wrong',
        meta: { title: 'Wrong', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/review-plan',
    component: Layout,
    name: 'ReviewPlan',
    meta: { title: 'ReviewPlan', icon: 'dashboard' },
    children: [
      {
        path: 'plan',
        component: () => import('@/views/review-plan/plan/index'),
        name: 'Plan',
        meta: { title: 'Plan', icon: 'dashboard' }
      },
      {
        path: 'schedule',
        component: () => import('@/views/review-plan/schedule/index'),
        name: 'Schedule',
        meta: { title: 'Schedule', icon: 'dashboard' }
      },
      {
        path: 'remind',
        component: () => import('@/views/review-plan/remind/index'),
        name: 'Remind',
        meta: { title: 'Remind', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
